package com.wangyu.springsecuritydemo.repository.primary;

import com.wangyu.springsecuritydemo.entity.primary.RoleEntity;
import com.wangyu.springsecuritydemo.repository.BaseRepository;
import java.util.Set;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

/**
 * 角色数据持久层
 *
 * @author wangyu
 * @Date 2019/2/19 9:52
 * @Version 1.0
 **/
public interface RoleRepository extends BaseRepository<RoleEntity> {

  /**
   * 根据用户ID查询所属角色列表
   *
   * @param userId 用户ID
   */
  @Query("from RoleEntity r left join fetch r.users u where u.id=:userId")
  Set<RoleEntity> findByUserId(@Param("userId") String userId);

  /**
   * 根据权限ID查询角色信息
   *
   * @param permissionId 权限ID
   */
  @Query("from RoleEntity r left join fetch r.permissions p where p.id =:permissionId")
  Set<RoleEntity> findByPermissionId(@Param("permissionId") String permissionId);
}
